package com.chima.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import com.chima.domain.DetailPost;
import com.chima.domain.Employees;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.service.DetailPostService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import flexjson.JSONSerializer;

public class DetailPostAction extends ActionSupport implements ModelDriven<DetailPost>{
	DetailPost detailPost= new DetailPost();
	public DetailPost getModel() {
		return detailPost;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private boolean success=false ;
	private DetailPostService detailPostService;
	private List< DetailPost> detailPosts;
	private int department;//查询条件
	private int post1;//查询条件
	private int serchemp;
	
	public int getSerchemp() {
		return serchemp;
	}

	public void setSerchemp(int serchemp) {
		this.serchemp = serchemp;
	}

	public int getPost1() {
		return post1;
	}

	public void setPost1(int post1) {
		this.post1 = post1;
	}

	public int getDepartment() {
		return department;
	}

	public void setDepartment(int department) {
		this.department = department;
	}

	public DetailPostService getDetailPostService() {
		return detailPostService;
	}

	public void setDetailPostService(DetailPostService detailPostService) {
		this.detailPostService = detailPostService;
	}

	public List<DetailPost> getDetailPosts() {
		return detailPosts;
	}

	public void setDetailPosts(List<DetailPost> detailPosts) {
		this.detailPosts = detailPosts;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String ListDetailPost() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(DetailPost.class);
		
  		if(serchemp!=0){
  			criteria.add(Restrictions.eq("employee.id",serchemp));
  		}
        pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		pageRequestBean.setEmp(serchemp);
		PageResponseBean pageResponseBean = detailPostService.findDetailPost(pageRequestBean);
		detailPosts=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        String resultJson1 = jsonSerializer.deepSerialize(detailPosts);  
//        jsonSerializer.exclude("*.class","*.post");
        jsonSerializer.exclude("*.class");
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",detailPosts:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	public void addDetailPost() throws Exception{
		try {
//			Employees e=detailPost.getEmployee();
			DetailPost p= new DetailPost();
		//	p.setName(detailPost.getName());
			p.setPost(detailPost.getPost());
			p.setPostHour(detailPost.getPostHour());
//			p.setAdcontent(detailPost.getAdcontent());
			p.setEmployee(detailPost.getEmployee());
			p.setDepartment(detailPost.getDepartment());
			p.setSonPost(detailPost.getSonPost());
//			p.setDetailPostCoffe(detailPost.getDetailPostCoffe());
			detailPostService.saveDetailPost(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	
	public void editDetailPost() throws Exception{
	detailPostService.updateDetailPost(detailPost);
	this.success=true;
	JSONSerializer jsonSerializer = new JSONSerializer();
	String resultJson =jsonSerializer.serialize(success);
	String jsonString = "{success:"+resultJson.toString()+"}";
	System.out.println(jsonString);
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public String deleteDetailPost(){
		
		   
			detailPostService.deleteDetailPost(detailPost.getId());
		return NONE;
	}
	public String ListlplanDetailPost()throws Exception{
		Employees e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		if(post1!=0){
			detailPosts=detailPostService.findByPostAndEmp(post1, e.getId());
		}
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class","*.jobDetails","*.records","*.jobs","*.employees","*.weights");
        String resultJson1 = jsonSerializer.deepSerialize(detailPosts);  
        String jsonString = "{detailPosts:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }

}
